_PYTHON_CONVERT_SHEBANG_BASE_PART_REGEX='^#![[:space:]]*([^[:space:]]*/usr/bin/env[[:space:]]+)?([^[:space:]]*/)?(jython|pypy-c|python)'

python-convert_convert_shebangs() {
        local argument file files=() only_executables="0" python_interpreter quiet="0" recursive="0" shebangs_converted="0"

        while (($#)); do
                case "$1" in
                        -r|--recursive)
                                recursive="1"
                                ;;
                        -q|--quiet)
                                quiet="1"
                                ;;
                        -x|--only-executables)
                                only_executables="1"
                                ;;
                        --)
                                shift
                                break
                                ;;
                        -*)
                                die "${FUNCNAME}(): Unrecognized option '$1'"
                                ;;
                        *)
                                break
                                ;;
                esac
                shift
        done

        if [[ "$#" -eq 0 ]]; then
                die "${FUNCNAME}(): Missing Python interpreter and files or directories"
        elif [[ "$#" -eq 1 ]]; then
                die "${FUNCNAME}(): Missing files or directories"
        fi

        python_interpreter=$1

        for argument in "$@"; do
                if [[ ! -e "${argument}" ]]; then
                        die "${FUNCNAME}(): '${argument}' does not exist"
                elif [[ -f "${argument}" ]]; then
                        files+=("${argument}")
                elif [[ -d "${argument}" ]]; then
                        if [[ "${recursive}" == "1" ]]; then
                                while read -d $'\0' -r file; do
                                        files+=("${file}")
                                done < <(find "${argument}" $([[ "${only_executables}" == "1" ]] && echo -perm /111) -type f -print0)
                        else
                                die "${FUNCNAME}(): '${argument}' is not a regular file"
                        fi
                else
                        die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
                fi
        done

        for file in "${files[@]}"; do
                file="${file#./}"
                [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue

                if [[ "$(head -n1 "${file}")" =~ ${_PYTHON_CONVERT_SHEBANG_BASE_PART_REGEX} ]]; then
                        [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue

                        shebangs_converted="1"

                        if [[ "${quiet}" == "0" ]]; then
                                einfo "Converting shebang in '${file}'"
                        fi

                        sed -e "1s:^#![[:space:]]*\([^[:space:]]*/usr/bin/env[[:space:]]\)\?[[:space:]]*\([^[:space:]]*/\)\?\(jython\|pypy-c\|python\)\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?\(\$\|[[:space:]].*\):#!\1\2${python_interpreter}\6:" -i "${file}" || die "Conversion of shebang in '${file}' failed"
                fi
        done

        if [[ "${shebangs_converted}" == "0" ]]; then
                ewarn "${FUNCNAME}(): Python scripts not found"
        fi
}

